package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.admin.yuejian21.Model.ActivityModel;
import com.example.admin.yuejian21.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaoMingActivity extends AppCompatActivity {
    private Button act_buy;
    private ImageView act_back;
    private EditText placeordername,placeorderphone,placeordernotice,adultnum,studentnum;
    private TextView pay;
    private String act_id;
    private String str_name;
    private String str_tel;
    private String str_paybotice,adunum,stunum,str_adultnum,str_studentnum,str_allpay;
    private Double allprice,stuprice,aduprice,num_adultnum,num_studentnum;
    private int user_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bao_ming);
        initViews();
        initEvents();
    }
    private void initViews(){
        placeordername= (EditText) findViewById(R.id.placeorder_name);
        placeorderphone= (EditText) findViewById(R.id.train_phone);
        adultnum= (EditText) findViewById(R.id.num_adultnum) ;
        studentnum = (EditText) findViewById(R.id.num_studentnum);
        placeordernotice = (EditText) findViewById(R.id.notice);
        act_back = (ImageView) findViewById(R.id.act_back);
        act_buy = (Button) findViewById(R.id.act_buy);
        pay = (TextView) findViewById(R.id.pay);
        Intent intent = getIntent();
        act_id = intent.getStringExtra("act_id");
        aduprice = Double.valueOf(intent.getStringExtra("aduprice"));
        stuprice = Double.valueOf(intent.getStringExtra("stuprice"));
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        adunum = adultnum.getText().toString();
        stunum = studentnum.getText().toString();
        num_adultnum = Double.parseDouble(adunum);
        num_studentnum= Double.parseDouble(stunum);
        allprice = num_adultnum*aduprice+num_studentnum*stuprice;
        pay.setText(allprice.toString()+"元");
    }
    private void initEvents() {
        act_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        act_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buy();
            }
        });

    }
    private void buy(){
        str_name = placeordername.getText().toString();
        str_tel = placeorderphone.getText().toString();
        str_paybotice = placeordernotice.getText().toString();
        str_adultnum= adultnum.getText().toString();
        str_studentnum = studentnum.getText().toString();
        str_allpay = allprice.toString();
        ActivityModel activityModel = new ActivityModel();
        Call<Integer> call = activityModel.signAct(str_name,act_id,str_tel,str_adultnum,str_studentnum,"1",str_allpay,str_paybotice,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()!=0){
                    Toast.makeText(BaoMingActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(BaoMingActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(BaoMingActivity.this,"获取连接失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}


